package com.nulldev.util.java;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

public final class TypeUtils {
	public final static class Hex {
	    private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

	    public static String encodeHex(byte[] data) {
	        return encodeHex(data, 0);
	    }

	    public static String encodeHex(ByteBuffer data) {
	        ByteBuffer byteBuffer = data.duplicate();
	        StringBuilder sb = new StringBuilder();
	        while (byteBuffer.remaining() > 0) {
	            byte b = byteBuffer.get();
	            sb.append(DIGITS[(0xF0 & b) >>> 4]);
	            sb.append(DIGITS[0x0F & b]);
	        }
	        return sb.toString();
	    }

	    public static String encodeHex(byte[] data, int group) {
	        int l = data.length;
	        char[] out = new char[(l << 1) + (group > 0 ? (l / group) : 0)];
	        // two characters form the hex value.
	        for (int i = 0, j = 0; i < l; i++) {
	            if ((group > 0) && ((i % group) == 0) && j > 0) {
	                out[j++] = '-';
	            }

	            out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
	            out[j++] = DIGITS[0x0F & data[i]];
	        }
	        return new String(out);
	    }


	    public static byte[] decodeHex(String hexString) {
	        ByteArrayOutputStream bas = new ByteArrayOutputStream();
	        for (int i = 0; i < hexString.length(); i += 2) {
	            int b = Integer.parseInt(hexString.substring(i, i + 2), 16);
	            bas.write(b);
	        }
	        return bas.toByteArray();
	    }
	}
	
	public final static class UTF8 {
	    public static byte[] convert(String s) {
	        try {
	            if (s != null) {
	                return s.getBytes("UTF-8");
	            } else {
	                return null;
	            }
	        } catch (UnsupportedEncodingException e) {
	            throw new Error(e);
	        }
	    }

	    public static String convert(byte[] b) {
	        try {
	            if (b != null) {
	                return new String(b, "UTF-8");
	            } else {
	                return null;
	            }
	        } catch (UnsupportedEncodingException e) {
	            throw new Error(e);
	        }
	    }

	    public static int utf8StringLengthInBytes(String utf8) {
	        try {
	            if (utf8 != null) {
	                return utf8.getBytes("UTF-8").length;
	            } else {
	                return 0;
	            }
	        } catch (UnsupportedEncodingException e) {
	            throw new RuntimeException();
	        }
	    }
	}
	
    /**
     * Casts a long to an int. In many cases I use a long for a UInt32 but this cannot be used to allocate
     * ByteBuffers or arrays since they restricted to <code>Integer.MAX_VALUE</code> this cast-method will throw
     * a RuntimeException if the cast would cause a loss of information.
     *
     * @param l the long value
     * @return the long value as int
     */
    public static int l2i(long l) {
        if (l > Integer.MAX_VALUE || l < Integer.MIN_VALUE) {
            throw new RuntimeException("A cast to an int has gone wrong. (" + l + ")");
        }
        return (int) l;
    }
}
